*****************************************************************
*****************************************************************
*** 0. General settings
*****************************************************************
*****************************************************************

* clear
clear
clear 		mata
mat 		drop _all
clear 		matrix

* set path
global		path	""				

set maxvar 32767






* load call report data and eliminate missings
use "$path/0 Data/_Processed/BHC_Bank_Ratios.dta", clear
duplicates drop permco yq, force
drop if permco == .
save "$path/0 Data/_Processed/BHC_Bank_Ratios_no_empty_permco.dta", replace



******** Covid Crisis analysis - quarterly 



* load quarterly stock returns
use "$path/0 Data/CRSP/US_Bank_log_returns_quarterly_1995-2020.dta", clear

* merge with quarterly FF risk factors
merge m:1 quarter using "$path/0 Data/_Processed/FF_Risk_Factors_quarterly.dta"
drop if _merge != 3
drop _merge

* merge with call report data on BHCs
rename quarter yq
* 6 weird duplicates to be dropped
duplicates drop permco yq, force
merge 1:1 permco yq using "$path/0 Data/_Processed/BHC_Bank_Ratios_no_empty_permco.dta", force
drop _merge


* define crisis dummy
rename yq quarterly_date
gen crisis = (quarterly_date == 240)
gen pre_crisis = 0
replace pre_crisis = 1 if quarterly_date < 240
gen post_crisis = 0
replace post_crisis = 1 if quarterly_date > 240

* generate credit line commitment variable
gen unused_ci_assets = offbs_unused_loans/ assets_total
bysort permco (quarterly_date): gen unused_ci_assets_lag = unused_ci_assets[_n-1]

* drop observations before 2019
drop if quarterly_date < 236

* define above median commitment dummy using time-average
bysort permco: egen mean_commitment = mean(unused_ci_assets)
drop if mean_commitment == .
gen commitment_above_median = 0
sum mean_commitment, detail
replace commitment_above_median  = 1 if mean_commitment > `r(p50)'
replace commitment_above_median  = . if mean_commitment == .

*
	save "$path/0 Data/_Processed/temp/Data_for_Return_Through_Cycle_Tests", replace

	*only keep 2019Q4 for matching
	keep if quarterly_date == 239

	
	psmatch2 commitment_above_median npl_loans log_assets reg_tier1_ratio loans_assets, n(1) logit
	gen pair = _id if _treated==0
	replace pair = _n1 if _treated==1
	bysort pair: egen paircount = count(pair)
	drop if paircount <2
	
	keep entity commitment_above_median
	
	
	merge 1:n entity using "$path/0 Data/_Processed/temp/Data_for_Return_Through_Cycle_Tests"
	keep if _merge == 3
	drop _merge
*

* simple regression
xtset permco quarterly_date
*xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.unused_ci_assets_lag##c.crisis
eststo, title("Model 1"): xtreg quarterly_return c.commitment_above_median##c.crisis
eststo, title("Model 2a"): xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.commitment_above_median##c.crisis
eststo, title("Model 2b"): xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.commitment_above_median##c.crisis, fe vce(robust)


		local 		date:	di %tdCYND daily("$S_DATE", "DMY")

esttab using "$path/04 Results/`date'_Table8_cols1to3.csv", p(3) interaction(" x ") noomitted label star(* 0.10 ** 0.05 *** 0.01) stats(r2  N, labels(R-squared "Number obs."))  replace



******** GFC analysis


* load quarterly stock returns
use "$path/0 Data/CRSP/US_Bank_log_returns_quarterly_1995-2020.dta", clear

* merge with quarterly FF risk factors
merge m:1 quarter using "$path/0 Data/_Processed/FF_Risk_Factors_quarterly.dta"
drop if _merge != 3
drop _merge

* merge with call report data on BHCs
rename quarter yq
* 6 weird duplicates to be dropped
duplicates drop permco yq, force
merge 1:1 permco yq using "$path/0 Data/_Processed/BHC_Bank_Ratios_no_empty_permco.dta", force
drop _merge


* define crisis dummy
rename yq quarterly_date
gen crisis = (quarterly_date >= 190 & quarterly_date <= 197)
gen pre_crisis = 0
replace pre_crisis = 1 if quarterly_date < 190
gen post_crisis = 0
replace post_crisis = 1 if quarterly_date > 197


* generate credit line commitment variable
gen unused_ci_assets = offbs_unused_commitments_other/ assets_total
bysort permco (quarterly_date): gen unused_ci_assets_lag = unused_ci_assets[_n-1]

* drop observations after 2011 (2009) and before 2004
drop if quarterly_date > 207
*drop if quarterly_date > 197
drop if quarterly_date < 176

* define above median commitment dummy using time-average
bysort permco: egen mean_commitment = mean(unused_ci_assets)
drop if mean_commitment == .
gen commitment_above_median = 0
sum mean_commitment, detail
replace commitment_above_median  = 1 if mean_commitment > `r(p50)'
replace commitment_above_median  = . if mean_commitment == .


	save "$path/0 Data/_Processed/temp/Data_for_Return_Through_Cycle_Tests", replace

	*only keep 2007Q2 for matching
	keep if quarterly_date == 189

	
	psmatch2 commitment_above_median npl_loans log_assets reg_tier1_ratio loans_assets, n(1) logit
	gen pair = _id if _treated==0
	replace pair = _n1 if _treated==1
	bysort pair: egen paircount = count(pair)
	drop if paircount <2
	
	keep entity commitment_above_median
	
	
	merge 1:n entity using "$path/0 Data/_Processed/temp/Data_for_Return_Through_Cycle_Tests"
	keep if _merge == 3
	drop _merge
	
	

* simple regression
xtset permco quarterly_date
*xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.unused_ci_assets_lag##c.crisis
eststo, title("Model 1"): xtreg quarterly_return c.commitment_above_median##c.crisis
eststo, title("Model 2a"): xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.commitment_above_median##c.crisis
eststo, title("Model 2b"): xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.commitment_above_median##c.crisis, fe vce(robust)

		local 		date:	di %tdCYND daily("$S_DATE", "DMY")

esttab using "$path/04 Results/`date'_Table8_cols4to6.csv", p(3) interaction(" x ") noomitted label star(* 0.10 ** 0.05 *** 0.01) stats(r2  N, labels(R-squared "Number obs."))  replace





******** Dotcom bubble analysis


* load quarterly stock returns
use "$path/0 Data/CRSP/US_Bank_log_returns_quarterly_1995-2020.dta", clear

* merge with quarterly FF risk factors
merge m:1 quarter using "$path/0 Data/_Processed/FF_Risk_Factors_quarterly.dta"
drop if _merge != 3
drop _merge

* merge with call report data on BHCs
rename quarter yq
* 6 weird duplicates to be dropped
duplicates drop permco yq, force
merge 1:1 permco yq using "$path/0 Data/_Processed/BHC_Bank_Ratios_no_empty_permco.dta", force
drop _merge


* define crisis dummy
rename yq quarterly_date
gen crisis = (quarterly_date >= 164 & quarterly_date <= 167)
gen pre_crisis = 0
replace pre_crisis = 1 if quarterly_date < 164
gen post_crisis = 0
replace post_crisis = 1 if quarterly_date > 167


* generate credit line commitment variable
gen unused_ci_assets = offbs_unused_commitments_other/ assets_total
bysort permco (quarterly_date): gen unused_ci_assets_lag = unused_ci_assets[_n-1]

* drop observations after 2002 and before 2000
drop if quarterly_date > 171
drop if quarterly_date < 160

* define above median commitment dummy using time-average
bysort permco: egen mean_commitment = mean(unused_ci_assets)
drop if mean_commitment == .
gen commitment_above_median = 0
sum mean_commitment, detail
replace commitment_above_median  = 1 if mean_commitment > `r(p50)'
replace commitment_above_median  = . if mean_commitment == .


	save "$path/0 Data/_Processed/temp/Data_for_Return_Through_Cycle_Tests", replace

	*only keep 2000Q4 for matching
	keep if quarterly_date == 163

	gen equity_ratio = equity_capital_total/assets_total
	psmatch2 commitment_above_median log_assets equity_ratio loans_assets, n(1) logit
	gen pair = _id if _treated==0
	replace pair = _n1 if _treated==1
	bysort pair: egen paircount = count(pair)
	drop if paircount <2
	
	keep entity commitment_above_median
	
	
	merge 1:n entity using "$path/0 Data/_Processed/temp/Data_for_Return_Through_Cycle_Tests"
	keep if _merge == 3
	drop _merge
	
	

* simple regression
xtset permco quarterly_date
*xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.unused_ci_assets_lag##c.crisis
eststo, title("Model 1"): xtreg quarterly_return c.commitment_above_median##c.crisis
eststo, title("Model 2a"): xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.commitment_above_median##c.crisis
eststo, title("Model 2b"): xtreg quarterly_return quarterly_mktrf quarterly_smb_ff3 quarterly_hml c.commitment_above_median##c.crisis, fe vce(robust)


		local 		date:	di %tdCYND daily("$S_DATE", "DMY")

esttab using "$path/04 Results/`date'_Table8_cols7to9.csv", p(3) interaction(" x ") noomitted label star(* 0.10 ** 0.05 *** 0.01) stats(r2  N, labels(R-squared "Number obs."))  replace














